package com.fengmi.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.fengmi.dto.PageDTO;
import com.fengmi.entity.SysUser;
import com.fengmi.service.SysUserService;
import com.fengmi.vo.PageResultVO;
import com.fengmi.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.List;

/**
 * @Author 123
 * @Date 2022/1/25 10:05
 * @Version 1.0
 */

@RestController
@RequestMapping("user")
@CrossOrigin
@Api(tags = "用户信息管理")
public class SysUserController {

    @Autowired
    private SysUserService userService;

    @PostMapping("page")
    @ApiOperation("分页及条件查询")
    public PageResultVO<SysUser> page(@RequestBody PageDTO<SysUser> pageDTO) {
        return userService.findAllUserByPage(pageDTO);
    }

    @PostMapping("addUser")
    @ApiOperation("添加用户信息")
    public ResultVO page(@RequestBody SysUser sysUser) {
        return userService.addUser(sysUser);
    }

    @PutMapping("enable/{userId}/{enabled}")
    @ApiOperation("修改用户启用状态")
    public ResultVO updateUserEnabled(@PathVariable String userId, @PathVariable Integer enabled) {
        return userService.updateUserEnabled(userId, enabled);
    }

    @PutMapping("update")
    @ApiOperation("修改用户启用状态")
    public ResultVO updateUserEnabled(@RequestBody SysUser sysUser) {
        return userService.updateUserInfo(sysUser);
    }

    @GetMapping("role/{userId}")
    @ApiOperation("查询用户的角色")
    public ResultVO findUserRolesById(@PathVariable String userId) {
        return userService.selectUserRolesById(userId);
    }

    @PostMapping("allocRole/{userId}")
    @ApiOperation("角色分配权限")
    public ResultVO allotPermissions(@PathVariable String userId, @RequestBody String[] roleIds) {
        return userService.allocRolesToUser(userId, roleIds);
    }

    @DeleteMapping("del/{userId}")
    public ResultVO deleteUser(@PathVariable String userId) {
        return userService.deleteUser(userId);
    }

    @PostMapping("login")
    @ApiOperation("用户登录")
    public ResultVO login(@RequestBody SysUser sysUser) {
        return userService.login(sysUser);
    }

    @PostMapping("verify")
    @ApiOperation("令牌校验")
    public ResultVO verify(@RequestHeader String token) {
        return userService.verify(token);
    }

    @GetMapping("exportContent")
    @ApiOperation("导出用户表格信息")
    public void exportContent(HttpServletResponse response) {
        try {
            // 获取用户信息
            List<SysUser> userList = userService.findAll();
            //导出
            Workbook workbook = ExcelExportUtil.exportExcel(new ExportParams("用户信息", "平台用户信息")
                    , SysUser.class, userList);
            //设置响应
            response.setHeader("content-disposition", "attachment;fileName=" + URLEncoder.encode("平台用户列表.xls", "UTF-8"));
            response.setContentType("application/vnd.ms-excel;charset=utf-8");

            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);

            outputStream.close();
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @PostMapping("importExcel")
    @ApiOperation("上传用户信息表格导入数据库")
    public ResultVO importInfo(MultipartFile excelFile) {
        try {
            ImportParams params = new ImportParams();
            params.setHeadRows(1);
            params.setTitleRows(1);

            List<SysUser> users = ExcelImportUtil.importExcel(excelFile.getInputStream(),
                    SysUser.class, params);

            users.forEach(System.out::println);

            //todo :save to mysql


            return new ResultVO(true, "上传成功");
        } catch (Exception e) {
            e.printStackTrace();
            return new ResultVO(false, "上传失败");
        }
    }

}
